﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Demos.Puzzles.EightPuzzle
{
    static class ImageCutter
    {
        public static BitmapImage Cut(string ruta, int altoPieza, int anchoPieza, int fila, int columna)
        {
            BitmapImage bi = null;
            try
            {
                var x = (Bitmap)Image.FromFile(ruta);
                var EX = columna * anchoPieza;
                var EY = fila * altoPieza;
                var EH = altoPieza;
                var EW = anchoPieza;
                var x2 = x.Clone(new Rectangle(EX, EY, EW, EH), x.PixelFormat);

                var ms = new MemoryStream();
                x2.Save(ms, ImageFormat.Png);
                ms.Position = 0;
                bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = ms;
                bi.EndInit();
            }
            catch (Exception)
            {
                MessageBox.Show("Error al cargar imagen", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return bi;
        }
    }
}
